<?php
//get dates and terms
$creation_date = date('F d, Y', strtotime($this->item->getCreationDate())); 
$creation_term = new term();
$creation_term = $creation_term->getTermByDate($this->item->getCreationDate()) ? $creation_term->getTerm() : 'n/a';
$modification_date = date('F d, Y', strtotime($this->item->getLastModifiedDate()));
$modification_term = new term();
$modification_term = $modification_term->getTermByDate($this->item->getLastModifiedDate()) ? $modification_term->getTerm() : 'n/a';

//get creator (if electronic item), or home library (if physical item)
if($this->item->isPhysicalItem()) {	//physical
	$home_lib_id = $this->item->getHomeLibraryID();
	if(!empty($home_lib_id)) {
		$home_lib = new library($home_lib_id);
		$owner = $home_lib->getLibrary();
	}
	else {
		$owner = 'n/a';
	}
	$owner_label = 'Owning Library';			
}
else {	//electronic
	$item_audit = new itemAudit($this->item->getItemID());
	$creator_id = $item_audit->getAddedBy();
	if(!empty($creator_id)) {
		$creator = new user($creator_id);
		$owner = $creator->getName(false).' ('.$creator->getUsername().') &ndash; '.$creator->getUserClass();
	}
	else {
		$owner = 'n/a';
	}			
	$owner_label = 'Created By';
	
}

//get reserve history
$classes = $this->item->getAllCourseInstances();

//get history totals

//total # of classes
$total_classes = sizeof($classes);		
//total # of instructors
$instructors = array();
foreach($classes as $ci) {
	$ci->getInstructors();
	foreach($ci->instructorIDs as $instrID) {
		$instructors[] = $instrID;
	}
}
$instructors = array_unique($instructors);
$total_instructors = sizeof($instructors);
?>
	<div class="displayArea">
		<div class="headingCell1">ITEM ORIGIN <?php if (!is_null(($this->reserve))) {?>(Item Id <a href="./?cmd=editItem&itemID=<?php print($this->item->getId()); ?>&tab=history"><?php print($this->item->getId()); ?></a>)<?php } ?></div>
		<div id="item_origin" style="padding:8px 8px 12px 8px;">
			<div style="float:left; width:30%;">
				<strong>Item Created On:</strong>
				<br />
				<?php print($creation_date); ?> (<?php print($creation_term); ?>)
			</div>
			<div style="float:left; width:30%;">
				<strong><?php print($owner_label); ?>:</strong>
				<br />
				<?php print($owner); ?>
			</div>
			<div style="float:left; width:30%;">			
				<strong>Last Modified:</strong>
				<br />
				<?php print($modification_date); ?> (<?php print($modification_term); ?>)	
			</div>
			<div class="clear"></div>
		</div>
<?php if (!is_null(($this->reserve))) {
		$request = new request();
		$request->getRequestByReserveID($this->reserve->getId());
		
?>
<div class="headingCell1">RESERVE STATUS</div>
<?php if ($request && $request->getReserveID()) { ?>
		<div id="item_origin" style="padding:8px 8px 12px 8px;">
			<div class="grid_3">
				<strong>Request Created On:</strong>
				<br />
				<?php print(Filter_DbDate::filter($request->getDateRequested())); ?>
			</div>
			<div class="grid_6 prefix_1">
				<strong>Status History:</strong>
				<br />
				<ul>
				<?php foreach($request->getStatusHistory() as $entry) {?>
					<li><?php print($entry['label']); ?> : 
					<?php print(Filter_DbDate::filter($entry['time'])); ?> 
					<br/>by <?php print($entry['user_full_name']); ?>
					</li>
				<?php } ?>
				</ul>
			</div>
			<div class="grid_4 prefix_half">			
				<strong>Request Completed On:</strong>
				<br />
				<?php print(
					!is_null($request->getProcessedDate())
						&& '' != $request->getProcessedDate()
						&& Filter_DbDate::ZERO_DATE != $request->getProcessedDate()
					? Filter_DbDate::filter($request->getProcessedDate())
					: 'In Process'
					); ?>
			</div>
<?php } else { ?>
		<div id="item_origin" style="padding:8px 8px 12px 8px;">
			<div class="grid_3">
				<strong>Reserve Created On:</strong>
				<br />
				<?php print(Filter_DbDate::filter($this->reserve->getCreationDate(), TRUE, TRUE)); ?>
			</div>
<?php } ?>
			<div class="clear"></div>
		</div>
<?php } ?>
		<div class="headingCell1">CLASS HISTORY</div>
		<div id="item_history">
			<div style="padding:8px; border-bottom:1px solid #333333;">
				<strong>Total # of classes:</strong> <?php print($total_classes); ?>
				<br />
				<strong>Total # of instructors:</strong> <?php print($total_instructors); ?>
<!--				
				<br />
				<strong>Total times viewed (all semesters):</strong> ###
-->
			</div>
			
			<table width="100%" border="0" cellpadding="4" cellspacing="0">
				<tr class="headingCell2" align="left" style="text-align:left;">
					<td width="5%">&nbsp;</td>
					<td width="10%">Reserve ID</td>
					<td width="15%">Term</td>
					<td width="15%">Course Number</td>
					<td width="25%">Course Name</td>
					<td width="15%">Instructor</td>					
					<td width="5%">Status</td>
					<td width="10%">&nbsp;</td>					
				</tr>
<?php
			$rowIndex = 0;
			//loop through the courses
			foreach($classes as $ci){
				$ci->getPrimaryCourse();	//fetch the course object
				$ci->getInstructors();	//get a list of instructors
				$rowClass = ($rowIndex++ % 2 == 0) ? 'oddRow' : 'evenRow';
				
				$reserve = new reserve();
				$reserve->getReserveByCI_Item($ci->getCourseInstanceID(), $this->item->getItemID());
				$rowClass .= (
					!is_null($this->reserve) &&
						$reserve->getId() == $this->reserve->getId()
					? ' highlighted'
					: ''
				);
				//determine if this is a currently-active class
				$now = time();  //get current time
				//if given only the date, strtotime() assumes we mean start of day, ie YYYY-MM-DD 00:00:00
				//to make sure we include the whole expiration day, we'll make it YYYY-MM-DD 23:59:59
				if(($ci->getStatus()=='ACTIVE') && (strtotime($ci->getActivationDate()) <= $now) && (strtotime($ci->getExpirationDate().' 23:59:59') >= $now)) {
					$icon = '<img src="' . Rd_Layout::getIconUrl('book_asterisk') . '" alt="Active Reserve" />';
				}
				else {
					$icon = '&nbsp;';
				}
?>
				<tr class="<?php print($rowClass); ?>">
					<td class="centered"><?php print($icon); ?></td>
					<td class="centered"><a href="./?cmd=editItem&reserveID=<?php print($reserve->getId()); ?>&tab=history"><?php print($reserve->getId()); ?></a></td>
					<td><?php print($ci->displayTerm());?></td>
	    			<td><a target="_blank" href="./?cmd=editClass&ci=<?php print($ci->getId()); ?>"><?php print($ci->course->displayCourseNo());?></td></a>
					<td><?php print($ci->course->getName());?></td>
					<td><?php print($ci->displayInstructors());?></td>					
					<td><span class="<?php print(common_getStatusStyleTag($reserve->getStatusId()));?>"><?php print($reserve->getStatusLabel()); ?></span></td>
					<td style="text-align:center;"><a href="javascript:openWindow('no_control=1&cmd=previewReservesList&ci=<?php print($ci->getCourseInstanceID()); ?>','width=800,height=600');">preview</a></td>
				</tr> 
<?php		}	?>

			</table>
			<div style="padding:8px; border-top:1px solid #333333;">
				<span class="silent"><img src="<?php Rd_Layout::printIconUrl('book_asterisk'); ?>" alt=''/> = classes currently using this item </span>
			</div>
		</div>
	</div>